<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户简称" prop="merchantShortHand">
        <el-input
          v-model="queryParams.merchantShortHand"
          placeholder="请输入商户简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:BziMerchantInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:BziMerchantInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="routeSettle('/posp/SettleAccountInfo')"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >提交结算信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="routeSettle('/posp/bziLegalPersonInfo')"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >提交法人/个人信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="routeSettle('/posp/bziLicenseInfo')"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >提交营业执照信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="routeSettle('/posp/shopInfo')"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >提交门店信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="routeSettle('/posp/merchantFeeUpdate')"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >提费率信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="single"
          @click="submitMerchantApprove()"
          v-hasPermi="['system:BziMerchantInfo:edit']"
        >提交审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="BziMerchantInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="申请产品类型" align="center" prop="productType" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="商户类型" align="center" prop="merchantType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.merchant_type" :value="scope.row.merchantType"/>
        </template>
      </el-table-column>
      <el-table-column label="商户简称" align="center" prop="merchantShortHand" />
      <el-table-column label="MCC码" align="center" prop="mccCode" />
      <el-table-column label="联系方式" align="center" prop="contactPhoneNo" />
      <el-table-column label="客服电话" align="center" prop="clientServicePhoneNo" />
      <el-table-column label="商户名称" align="center" prop="merchantName" />
      <el-table-column label="注册人姓名" align="center" prop="applicant" />
      <el-table-column label="商户使用名" align="center" prop="merchantUserName" />
      <el-table-column label="商户子类型" align="center" prop="merchantSubType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.merchant_sub_type" :value="scope.row.merchantSubType"/>
        </template>
      </el-table-column>
      <el-table-column label="邀请机构" align="center" prop="userId" />
      <el-table-column label="操作" align="left" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:BziMerchantInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:BziMerchantInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="申请产品类型" prop="productType">
          <el-select multiple v-model="productTypes" placeholder="请选择产品类型">
            <el-option
              v-for="dict in dict.type.product_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="商户类型" prop="merchantType">
          <el-select v-model="form.merchantType" placeholder="请选择商户类型">
            <el-option
              v-for="dict in dict.type.merchant_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户简称" prop="merchantShortHand">
          <el-input v-model="form.merchantShortHand" placeholder="请输入商户简称" />
        </el-form-item>
        <el-form-item label="MCC码" prop="mccCode">
          <el-input v-model="form.mccCode" placeholder="请输入MCC码，银联标准" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactPhoneNo">
          <el-input v-model="form.contactPhoneNo" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="客服电话" prop="clientServicePhoneNo">
          <el-input v-model="form.clientServicePhoneNo" placeholder="请输入客服电话" />
        </el-form-item>
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="省份编码" prop="provinceNo">
          <el-select v-model="form.provinceNo" placeholder="请选择省份">
            <el-option
              v-for="item in provinceOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
              @click.native="getRegionChildTwo(item)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市编码" prop="cityNo">
          <el-select v-model="form.cityNo" placeholder="请选择城市">
            <el-option
              v-for="item in cityOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode"
              @click.native="getRegionChildThree(item)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区县代码" prop="areaNo">
          <el-select v-model="form.areaNo" placeholder="请选择区县">
            <el-option
              v-for="item in areaOptions"
              :key="item.areaCode"
              :label="item.areaName"
              :value="item.areaCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册人姓名" prop="applicant">
          <el-input v-model="form.applicant" placeholder="请输入注册人姓名" />
        </el-form-item>
        <el-form-item label="商户使用名" prop="merchantUserName">
          <el-input v-model="form.merchantUserName" placeholder="请输入商户使用名" />
        </el-form-item>
        <el-form-item label="商户子类型" prop="merchantSubType">
          <el-select v-model="form.merchantSubType" placeholder="请选择商户子类型">
            <el-option
              v-for="dict in dict.type.merchant_sub_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构邀请码" prop="merchantUserName">
          <el-input v-model="form.organizationCode" placeholder="请输入机构邀请码" />
        </el-form-item>
        <el-form-item label="机构密钥" prop="merchantUserName">
          <el-input v-model="form.organizationKey" placeholder="请输入机构KEY" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBziMerchantInfo, getBziMerchantInfo, delBziMerchantInfo, addBziMerchantInfo, updateBziMerchantInfo,getRegionRoot,submitMerchantApprove } from "@/api/posp/BizMerchantInfo";

export default {
  name: "BziMerchantInfo",
  dicts: ['merchant_type','merchant_sub_type','product_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户基本信息表格数据
      BziMerchantInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantShortHand: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      provinceOptions: {},
      cityOptions:{},
      areaOptions:{},
      productTypes:[]
    };
  },
  created() {
    this.getList();
    this.getRegionFirst();
  },
  methods: {
    submitMerchantApprove(){
      submitMerchantApprove(this.ids[0]).then(response => {
        console.log(response.data);
      });
    },
    routeSettle(url){
      this.$router.replace({path: url, query: {merchantId: this.ids[0]}});
    },
    /** 获取地区 */
    getRegionFirst(){
      let rootObj = {areaLevel: '1'};
      getRegionRoot(rootObj).then(response => {
        this.provinceOptions = response.data;
      });
    },
    getRegionChildTwo(item){
      let rootObj = {areaLevel: '2',parentCode:item.areaCode};
      getRegionRoot(rootObj).then(response => {
        this.cityOptions = response.data;
        this.form.cityNo = null
        this.form.areaNo = null
      });
    },
    getRegionChildThree(item){
      let rootObj = {areaLevel: '3',parentCode:item.areaCode};
      getRegionRoot(rootObj).then(response => {
        this.areaOptions = response.data;
        this.form.reaaNo = null
      });
    },
    /** 查询商户基本信息列表 */
    getList() {
      this.loading = true;
      listBziMerchantInfo(this.queryParams).then(response => {
        this.BziMerchantInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        merchantId: null,
        phone: null,
        agentId: null,
        productType: null,
        address: null,
        merchantType: null,
        merchantShortHand: null,
        mccCode: null,
        contactPhoneNo: null,
        clientServicePhoneNo: null,
        merchantName: null,
        provinceNo: null,
        cityNo: null,
        areaNo: null,
        applicant: null,
        merchantUserName: null,
        merchantSubType: null,
        organizationCode: null,
        organizationKey: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.merchantId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const merchantId = row.merchantId || this.ids
      let cityO = {areaLevel: '2',parentCode:row.provinceNo};
      getRegionRoot(cityO).then(response => {
        this.cityOptions = response.data;
      });
      let areaO = {areaLevel: '3',parentCode:row.cityNo};
      getRegionRoot(areaO).then(response => {
        this.areaOptions = response.data;
      });
      getBziMerchantInfo(merchantId).then(response => {
        this.form = response.data;
        if(this.form.productType){
          this.productTypes = this.form.productType.split("_");
        }
        this.open = true;
        this.title = "修改商户基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productType = this.productTypes.join("_");
          if (this.form.merchantId != null) {
            updateBziMerchantInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBziMerchantInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const merchantIds = row.merchantId || this.ids;
      this.$modal.confirm('是否确认删除商户基本信息编号为"' + merchantIds + '"的数据项？').then(function() {
        return delBziMerchantInfo(merchantIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/BziMerchantInfo/export', {
        ...this.queryParams
      }, `BziMerchantInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
